package com.onedec.attendance.controller;


import com.onedec.attendance.entity.OnedecAttendance;
import com.onedec.attendance.service.AttendanceService;
import com.onedec.attendance.utils.AttendUtil;
import com.onedec.attendance.utils.DateUtils;
import com.onedec.attendance.utils.Page;
import com.onedec.attendance.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;


    @PostMapping("/attend")
    public Result addAttend(Double longitude, Double latitude, Double height, HttpServletRequest req) {
        System.out.println("打卡！");
        Date date = new Date();
        if (date.getHours() < 11) {
            System.out.println("进入早上打卡");
            System.out.println(AttendUtil.getDistance(longitude, latitude));
            if (AttendUtil.getDistance(longitude, latitude) <= 100 && height - AttendUtil.HEIGHT <= 20) {
                System.out.println("打卡成功！");
                OnedecAttendance attendance = new OnedecAttendance();
                attendance.setAttDate(date);
                attendance.setAttDatetime(date);
                attendance.setAttEmpId(2);
                attendance.setAttType("morning");
                try {
                    attendanceService.insertMorningAttend(attendance);
                    return Result.success();
                } catch (Exception e) {
                    e.printStackTrace();
                    return Result.fail("打卡出错！");
                }

            } else {
                System.out.println("打卡失败！");
                return Result.fail("打卡出错！");
            }
        } else {
            System.out.println("进入下午打卡");
            System.out.println(AttendUtil.getDistance(longitude, latitude));
            if (AttendUtil.getDistance(longitude, latitude) <= 100 && height - AttendUtil.HEIGHT <= 20) {
                OnedecAttendance attendance = new OnedecAttendance();
                try {
                    attendance.setAttDate(DateUtils.DATE_FORMAT.parse(DateUtils.DATE_FORMAT.format(date)));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                attendance.setAttDatetime(date);
                attendance.setAttEmpId(2);
                attendance.setAttType("afternoon");
                try {
                    attendanceService.insertMorningAttend(attendance);
                } catch (Exception e) {
                    e.printStackTrace();
                    return Result.fail("打卡出错！");
                }
                System.out.println("打卡成功！");
                return Result.success();
            } else {
                System.out.println("打卡失败！");
                return Result.fail("打卡出错！");
            }
        }
    }

    @GetMapping("/getLates")
    public Result getAllLates(String start, String end, Page page, Integer status) {
        if ("".equals(start)) {
            start = "2000-01-01";
        }
        if ("".equals(end)) {
            end = "2200-01-01";
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        System.out.println("参数为：" + status);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setLenient(false);
        map.put("start", "'" + start + "'");
        System.out.println("start:" + "'" + start + "'");
        map.put("end", "'" + end + "'");
        System.out.println("end:" + "'" + end + "'");
        map.put("pageIndex", page.getPageIndex());
        System.out.println("i" + page.getPageIndex());
        map.put("number", page.getNumber());
        System.out.println("p" + page.getNumber());
        map.put("status", status);
        try {
            int count = attendanceService.selectAttendsCount(map);
            page.setDataCount(count);
            page.setPageCount((count - 1) / page.getNumber() + 1);
            List<Map<String, Object>> list = attendanceService.selectAttends(map);
//            for (Map<String, Object> attend : list) {
//                System.out.println(attend.get("att_datetime"));
//                java.util.Date timeDate = df.parse(attend.get("att_datetime") + "");
//                java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());
//                attend.put("att_datetime", df.format(dateTime));
//            }
            System.out.println(list);
            return Result.success(page, list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询失败！");
        }
        // 修改时间格式


    }
}
